import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


@SuppressWarnings("unused")
class Click{final static int ADD=0, DEL=1, EX=2,CLEAR=3;}

public class PlayList implements ActionListener{
	JFrame listFrame;
	Icon titleImage= new ImageIcon("image/base.png"); ;
    FileDialog flg;
	JScrollPane scrollPane;
	JPanel listPanel= null;
	JTable listTable = null;
	DefaultTableModel model;
	JButton buttonAdd;
	JButton buttonDelete;
	JButton buttonExtend;
	JButton buttonClear;
	JLabel listGround = null,
				   backGround = null;
	public int pathCurrent, pathNum;
	String[] listTitle={"FileName","TimeLength"};
	String[] listDirect={"","","","",
						 "","","","",
						 "","","","",
						 "","","",""};
	Object[][] listDatas={{"",""},{"",""},{"",""},{"",""},
						  {"",""},{"",""},{"",""},{"",""},
						  {"",""},{"",""},{"",""},{"",""},{"",""},{"",""},{"",""},{"",""}};
	
	 final String lstCmd[]={"buttonAdd","buttonDelete","buttonExtend","buttonClear"};
	
	public PlayList()
	{
		//super();
		listFrame = new JFrame("Playlist");
		listFrame.setVisible(false);
		listFrame.setLayout(new BorderLayout());
		listPanel = new JPanel();
		model = new DefaultTableModel(listTitle, 0);
		listTable = new JTable(model);
        scrollPane = new JScrollPane(listTable);
        scrollPane = new JScrollPane(listTable, 
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		listGround = new JLabel();
		backGround = new JLabel();
			
		buttonAdd = new JButton("add");;
		buttonDelete = new JButton("del");;
		buttonExtend= new JButton("ex");;
		buttonClear = new JButton("clear");
		
        buttonAdd.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonDelete.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonExtend.setPreferredSize(new java.awt.Dimension(64, 64));
        buttonClear.setPreferredSize(new java.awt.Dimension(64, 64));
        
        buttonAdd.setActionCommand("buttonAdd");;
        buttonDelete.setActionCommand("buttonDelete");;
        buttonExtend.setActionCommand("buttonExtend");;
        buttonClear.setActionCommand("buttonClear");
        
        buttonAdd.addActionListener(this);
        buttonDelete.addActionListener(this);
        buttonExtend.addActionListener(this);
        buttonClear.addActionListener(this);
        
        listPanel.add(buttonAdd);
		listPanel.add(buttonDelete);
		//listPanel.add(buttonExtend);
		//listPanel.add(buttonClear);
		
		listGround.setLayout(null);
		backGround.setLayout(null);
		JPanel topGround = new JPanel();
		topGround.setPreferredSize(new java.awt.Dimension(240, 36));
		topGround.add(backGround);
		listFrame.add(topGround,BorderLayout.NORTH);
		backGround.add(listPanel);
		backGround.setIcon(titleImage);
		listPanel.add(listGround);
		listFrame.add(scrollPane,BorderLayout.CENTER);
		listFrame.add(listPanel,BorderLayout.SOUTH);
		listTable.setVisible(true);
		listFrame.setSize(300, 600);
		listFrame.setLocation(250, 250);
		listFrame.setVisible(true);
	}


	
	public int listAdd(int pathNow, int pathSum, String filename, String directory,String time)
	{
		listDatas[pathNow][0] = filename;
		listDatas[pathNow][1] = time;
		listDirect[pathNow] = directory;
		pathCurrent = pathNow;
		pathNum = pathSum;
		model.addRow(listDatas[pathNow]);
		return pathCurrent;
	}
	
	public void listDel(int num)
	{
		model.removeRow(num);
		pathNum--;
		if(pathCurrent>=pathNum){
			pathCurrent--;
		}

	}
	
	public void listClear(){
		model = new DefaultTableModel(listTitle, 0);
		listTable = new JTable(model);
		pathCurrent= -1;
		pathNum = 0;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
        String cmd = e.getActionCommand();
        int id = -1;
        for (int i = 0; i <lstCmd.length; i++)
            if(lstCmd[i] == cmd) id=i;
        switch (id) {
        	case Click.ADD:
        		flg = new FileDialog( new Frame(), "Open", FileDialog.LOAD);
            	flg.setVisible(true);
                if(!flg.getFile().equals("null")){
                    pathNum++;//add path number
                    int pathNew = pathNum-1;//push path pointer to the last place
                	//AudioLoad();
                    String filePath = flg.getDirectory()+flg.getFile();
                    //f.setTitle(title));

                    AudioInputStream  audioInputStream = null;
                    File file = null;
                    //AudioFormat audioFormat = null;
                    file = new File(filePath);
                    int soundMinutes;
					int soundSeconds;
					try {
						audioInputStream = AudioSystem.getAudioInputStream(file);
					} catch (UnsupportedAudioFileException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
                        
            //get time length of the file(in M:S format)
                        long fileLength = audioInputStream.getFrameLength();
                        float lengthPerSecond = audioInputStream.getFormat().getSampleRate();
                        int totalSeconds = (int) (fileLength/lengthPerSecond);
                        soundMinutes = totalSeconds/60;
                        soundSeconds = totalSeconds%60;
                    
                	listAdd(pathNew,pathNum, flg.getFile(), flg.getDirectory(), soundMinutes+":"+soundSeconds);
                }
        		break;
        	case Click.DEL:
        		if(pathNum>0)
        		listDel(pathNum-1);
        		break;
        	case Click.EX:
        		break;
        	case Click.CLEAR:
        		//listClear();
        		break;
        	default:;
        }
		
	}

}
